import { createRouter, createWebHistory } from 'vue-router'
import { useRoute } from 'vue-router'
import store from '@/store'
// import VueRouter from 'vue-router';

// let originPush = createRouter.prototype.push;
// let originReplace = createRouter.prototype.replace;
// // 重写push|replace 方法
// // 第一个参数是告诉原来的push方法，你要往哪里跳转（传递哪些参数）
// // 第二个参数，成功的回调，第三个参数，失败的回调
// createRouter.prototype.push = function(location, resolve, reject) {
//     if (resolve && reject) { originPush.call(this, location) } else {
//         originPush.call(this, location, () => {}, () => {})
//     }
// }


// createRouter.prototype.replace = function(location, resolve, reject) {
//     if (resolve && reject) { originReplace.call(this, location) } else {
//         originReplace.call(this, location, () => {}, () => {})
//     }
// }


import routes from './routers'
import routersAdmin from './routersAdmin'
import routersStudent from './routersStudent'
import routersTeacher from './routersTeacher'
import nprogress from "nprogress"; //进度条
import "nprogress/nprogress.css" //引进进度条样式
const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    nprogress.start() //进度条

    if (to.path == '/login') {
        sessionStorage.clear('USERID')
        store.dispatch('getUserId')
        next()
    }
    // 判断是否登录
    if (store.state.user.userid == null) {
        // 不是登录的路由 不跳转
        if (to.path != '/login') {
            next({ path: '/login' })
        } else {
            next()
        }
    } else {
        // addRoute的加入路由，刷新就会消失，所以此时需要重新加入路由
        // 判断vuex的addRouters是否为空，若为空 动态路由未加入（刷新后/初始化） 一定要有一个条件，不然会陷入死循环
        if (store.state.user.addRouters.length == 0) {
            // 判断权限
            if (store.state.user.userid == 1) {
                // 派发action存动态路由
                store.dispatch('generateRoutes', routersStudent)
                routersStudent.forEach(route => {
                    router.addRoute(route)
                });
                // 首次跳入的页面 nav1
                if (to.path == '/home') {
                    next('/home/info')
                } else {
                    // 加入动态路由后，继续进行跳转
                    next({...to, replace: true })
                }
            } else
            if (store.state.user.userid == 2) {
                store.dispatch('generateRoutes', routersTeacher)
                routersTeacher.forEach(route => {
                    router.addRoute(route)
                });
                if (to.path == '/home') {
                    next('/home/info')
                } else {
                    // 加入动态路由后，进行跳转
                    next({...to, replace: true })
                }
            } else
            if (store.state.user.userid == 3) {
                store.dispatch('generateRoutes', routersAdmin)
                routersAdmin.forEach(route => {
                    router.addRoute(route)
                });
                if (to.path == '/home') {
                    next('/home/info')
                } else {
                    // 加入动态路由后，进行跳转
                    next({...to, replace: true })
                }
            }
        } else {
            // 若addRouters是不为空，则是正常路由跳转(没有进行刷新操作)
            nprogress.done();
            next()
        }
    }

})

export default router